package cn.lzd.zook;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

/**
 * Created by lzd on 2018/7/24.
 * 带监听
 */
public class Zk2 {
    private static String connectString = "hdpn:2181";
    private static int sessionTimeout = 2000;
    private static ZooKeeper zkClient = null;

    public static void main(String[] args) throws KeeperException, InterruptedException {
        try {
            zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    System.out.println("进来了。。。");
                    Event.KeeperState state = event.getState();
                    String path = event.getPath();
                    Event.EventType type = event.getType();
                    System.out.println(state + "\t" + path + "\t" + type);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

        byte[] data = zkClient.getData("/idea", false, null);
        System.out.println(new String(data));
    }
}
